<script setup>
import {ArrowDown, Search} from '@element-plus/icons-vue'
import {ref, watch} from 'vue'
import useLoginStore from '@/stores/user/index.js'
import {removeLocalStore} from '@/utils/localUser.js'
import router from '@/router/index.js'
import useArticleStore from '@/stores/article/index.js'

const loginStore = useLoginStore()
const articleStore = useArticleStore()

const searchParam = ref()

watch(searchParam, (newValue, oldValue) => {
  articleStore.searchParam = newValue
})

const register = () => {
  loginStore.isShow = true
  loginStore.info = '立即注册'
}

//退出登录
const logoutBtn = () => {
  removeLocalStore('token')
  //再次刷新页面
  location.reload()
  router.go(0)
}

//搜索文章列表
const searchBtn = () => {
  articleStore.getNewsListAction(1, 10, articleStore.searchParam, articleStore.channelId)
}

//发布文章
const publishArticle = async () => {
  await router.push('/publish')
}

//个人中心页面
const personInfoBtn = async () => {
  await router.push('/article-list')
}
//个人中心页面
const userInfoBtn = () => {
  router.push('/user')

}
</script>

<template>

  <div class="header">
    <video src="@/assets/video/banner.mp4" loop muted autoplay>
    </video>
    <div class="top">
      <div class="right">
        <div class="publish" @click="publishArticle">
          <img src="@/assets/images/add.png"/>
          <span>发布作品</span>
        </div>
        <div class="login">
          <el-button v-if="!loginStore.token" type="danger" @click="register">注册</el-button>
          <div class="info" v-else>
            <el-dropdown>
              <span class="link">
<!--                <img v-if="loginStore.userInfo.image" :src="loginStore.userInfo.image"/>-->
                <img :src="loginStore.personInfo.imageUrl"/>
                <span>{{ loginStore.userInfo.name }}</span>
                <el-icon class="el-icon--right">
                  <arrow-down/>
                </el-icon>
              </span>
              <template #dropdown>
                <el-dropdown-menu>
                  <el-dropdown-item @click="userInfoBtn">个人主页</el-dropdown-item>
                  <!--                  <el-dropdown-item @click="personInfoBtn">作品管理</el-dropdown-item>-->
                  <el-dropdown-item @click="logoutBtn">退出登录</el-dropdown-item>
                </el-dropdown-menu>
              </template>
            </el-dropdown>
          </div>
        </div>
      </div>
    </div>
    <div class="search">
      <h1>时事咨讯</h1>
      <el-input v-model="searchParam" style="max-width: 600px" placeholder="请输入关键字">
        <template #append>
          <el-button :icon="Search" @click="searchBtn"/>
        </template>
      </el-input>
    </div>
  </div>
</template>

<style scoped lang="less">
.header {

  video {
    position: absolute;
    z-index: -999;
    width: 100%;
  }

  //background-image: url('@/assets/images/banner.jfif');
  //background-size: cover;
  height: 270px;

  .right {
    padding: 10px 20px;
    display: flex;
    justify-content: right;
    align-items: center;

    .publish {
      display: flex;
      align-items: center;
      justify-content: center;
      cursor: pointer;

      img {
        width: 18px;
        height: 18px;
      }

      span {
        margin-left: 5px;
        font-size: 14px;
        color: #fff;
      }
    }

    .login {
      cursor: pointer;
      margin-left: 40px;

      .el-button {
        width: 52px;
        height: 28px;
      }

      .info {
        .link {
          display: flex;
          align-items: center;
        }

        img {
          border-radius: 50%;
          width: 26px;
          height: 26px;
        }

        span {
          margin-left: 5px;
          color: #fff;
        }
      }
    }
  }

  .search {
    .el-input {
      height: 48px;
    }

    display: flex;
    flex-direction: column;
    align-items: center;

    h1 {
      margin-bottom: 20px;
      color: #fff;
    }

    .el-button {
      width: 72px;
      height: 48px;
      background-color: #f04142;
    }

    :deep(.el-icon) {
      color: white;
    }
  }
}
</style>
